<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加训练课程|私教预约系统</title>
    <link href="../bootstrap-3.3.7-dist/css/bootstrap.min.css"  rel="stylesheet"  type="text/css" >
</head>

<body>

<h1 class="col-sm-offset-5">
    修改 <span class="label label-success">课程</span>信息</h1>
<form class="form-horizontal">
    <div class="form-group">

    </div>
    <div class="form-group">

    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">课程封面图片</label>
        <div class="col-sm-5">
            <img id="inputCoursePhoto" alt="点击此处选择" class="img-rounded" style="width: 150px;height: 150px;" onclick="changeCourseImage()">
        </div>
    </div>

    <div class="form-group">
        <label for="inputCourseName" class="col-sm-2 control-label">课程名称</label>
        <div class="col-sm-5">
            <input type="text" class="form-control" id="inputCourseName" placeholder="必填">
        </div>
    </div>

    <div class="form-group">
        <label for="inputCourseHours" class="col-sm-2 control-label">课时</label>
        <div class="col-sm-2">
            <input type="text" class="form-control" id="inputCourseHours" placeholder="必填，单位小时">
        </div>
    </div>

    <div class="form-group">
        <label for="inputCourseType" class="col-sm-2 control-label">课程类型</label>
        <div class="col-sm-3">
            <select class = "selectpicker form-control" id = "inputCourseType"  title="请选择">

            </select>
        </div>
    </div>

    <div class="form-group">
        <label for="inputCoursePrice" class="col-sm-2 control-label">课程价格</label>
        <div class="col-sm-2">
            <input type="text" name="u_bir" class="form-control" height="100px" id="inputCoursePrice" placeholder="人民币">
        </div>
    </div>

    <div class="form-group">
        <label for="inputMaxStudentsNumber" class="col-sm-2 control-label">最大学生容量</label>
        <div class="col-sm-2">
            <input type="text" class="form-control" id="inputMaxStudentsNumber" placeholder="默认50人">
        </div>
    </div>

    <div class="form-group">
        <label for="inputStartTime" class="col-sm-2 control-label">开课日期</label>
        <div class="col-sm-3">
            <input type="date" name="u_bir" class="form-control" height="100px" id="inputStartTime" value="2021-07-01"/>
        </div>
    </div>

    <div class="form-group">
        <label for="inputEndTime" class="col-sm-2 control-label">结课日期</label>
        <div class="col-sm-3">
            <input type="date" name="u_bir" class="form-control" height="100px" id="inputEndTime" value="2021-07-01"/>
        </div>
    </div>

    <div class="form-group">
        <label for="inputCourseDescription" class="col-sm-2 control-label">课程描述</label>
        <div class="col-sm-8">
            <textarea class="form-control" rows="3" id="inputCourseDescription" placeholder="简单地介绍课程"></textarea>
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="button" id="signup" class="btn btn-info" onclick="editCourse()">修改</button>
        </div>
    </div>
</form>
</body>

<script src="../jquery/jquery-3.4.1.min.js"></script>
<script src="../bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script type="text/javascript">
    jQuery(function() {

        jQuery.post("/getAllCourseCodeInfo",function(rst){
            for (let id = 0; id < rst.length; ++id){
                jQuery("#inputCourseType").append("<option value='"+rst[id].courseTypeId+"'>" + rst[id].courseType + "</option>");
            }
            let courseIdNow = $.cookie("currentCourseId");
            jQuery.post("/getCourseInfoById", {"courseId": courseIdNow}, function (rstl){
                $("#inputCoursePhoto").attr("src", rstl.coursePhoto);
                $("#inputCourseName").val(rstl.courseName);
                $("#inputCourseHours").val(rstl.courseHours);
                $("#inputCoursePrice").val(rstl.coursePrice);
                $("#inputMaxStudentsNumber").val(rstl.courseMaxStudents);
                $("#inputStartTime").val(rstl.courseStartTime);
                $("#inputEndTime").val(rstl.courseEndDate);
                $("#inputCourseDescription").val(rstl.courseDescription);
                $("#inputCourseType").val(rstl.courseTypeId);
            })
        },"json");
    });

    function editCourse(){
        let js = getInfo();
        let notNull = check(js);
        /*let trainerPhone = $.cookie("trainerphone");
        let trainerId = $.cookie("trainerid");*/

        if (notNull){
            jQuery.post("/updateCourseMsg", js, function (rst) {
                if (rst){
                    alert("修改课程信息成功！");
                    window.location.reload();
                } else
                    alert("修改课程信息失败！");
            })
        }

    }

    function getInfo(){
        let name = jQuery("#inputCourseName").val();
        let hours = jQuery("#inputCourseHours").val();
        let options = $("#inputCourseType option:selected");
        let typeId = options.val();
        let type = options.text();
        let price = jQuery("#inputCoursePrice").val();
        let maxStudents = jQuery("#inputMaxStudentsNumber").val();
        let startTime = jQuery("#inputStartTime").val();
        let endTime = jQuery("#inputEndTime").val();
        let description = jQuery("#inputCourseDescription").val();
        let courseId = $.cookie("currentCourseId");
        return {"courseName": name, "courseHours": hours, "courseTypeId": typeId, "coursePrice": price,
            "courseMaxStudents": maxStudents, "courseStartTime": startTime, "courseEndTime": endTime,
            "courseDescription": description, "courseId": courseId};
    }

    function check(js) {
        let name = js.courseName.replace(/(^\s*)|(\s*$)/g,'');
        let hours = js.courseHours.replace(/(^\s*)|(\s*$)/g,'');
        let price = js.coursePrice.replace(/(^\s*)|(\s*$)/g,'');
        if(name==='' || name===undefined){
            alert("课程名称不可为空")
        }
        else if(hours ==='' || hours===undefined){
            alert("课时不可为空")
        }
        else if(price ==='' || price===undefined){
            alert("课程价格不可为空")
        }else {
            return true
        }
    }

    function changeCourseImage(){
        window.location.href="courseChangImage.html";
    }

</script>
</html>
